function SetSessionKeyVale(sKey,sValue){
    //TODO ！设置会话指定键的值！  设置session值
    var sRet='';
    $.ajax({
        type: "post",
        url: "/Manage/SetSession",
        data: {
            sKey:sKey,
            sValue:sValue
        },
        async: false,
        success: function(data) {
            sRet=data;
        }
    });
    return sRet;
}



function GetSessionKeyVale(sKey){
    //TODO ！设置会话指定键的值！  设置session值
    var sRet='';
    $.ajax({
        type: "post",
        url: "/Manage/GetSession",
        data: {
            sKey:sKey,
        },
        async: false,
        success: function(data) {
            sRet=data;
        }
    });
    return sRet;
}





function GetDate() {
        //TODO 获取客户机日期格式为：2016-05-09
        var myDate = new Date();
        var sMonth="00"+(myDate.getMonth()+1);
        sMonth=sMonth.substr(sMonth.length-2,2);
        var sDay="00"+(myDate.getDate());
        sDay=sDay.substr(sDay.length-2,2);
        return myDate.getFullYear()+"-"+sMonth+"-"+sDay;
    }


function formToJson(form){
    //TODO from转JSON
    var data = $('#'+form).serialize();
    data= decodeURIComponent(data,true);//防止中文乱码
    data = data.replace(/&/g, "','" );
    data = data.replace(/=/g, "':'" );
    data = "({'" +data + "'})" ;
    obj = eval(data);
    return obj;
}

function GetJsonArray(sJson,iRow) {
    //TODO 获取Json数组下标为iRow的值
    var aJson=eval(sJson);
    return aJson[iRow];
}




function getParameterByName(paramName) {
    //TODO 获取URL参数
    var args = new Object();
    var argsStr = location.search;  //获取URL参数字符串
    if (argsStr.length > 0) {
        argsStr = argsStr.substring(1);
        var nameValueArr = argsStr.split("&");  //多参数
        for (var i = 0; i < nameValueArr.length; i++) {
            var pos = nameValueArr[i].indexOf('=');
            if (pos == -1) continue; //如果没有找到就跳过
            var argName = nameValueArr[i].substring(0, pos); //提取name
            var argVal = nameValueArr[i].substring(pos + 1); //提取value
            args[argName] = unescape(argVal);
        }
        return args[paramName];
    }
}



function setCookie(cname,cvalue,min){
    var d = new Date();
    d.setTime(d.getTime()+(min*60*1000));  //加上时区标准时间
    var expires = "expires="+d.toGMTString();
    document.cookie = cname+"="+cvalue+"; "+expires;
}

function getCookie(cname){
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name)==0) { return c.substring(name.length,c.length); }
    }
    return "";
}

function checkCookie(cname){
    var cname=getCookie(cname);
    if(cname==''){
        alert('操作超时！请重新进入');
        window.location.href=mapurl('/userindex.html');
        return true;
    }else{
        return false;
    }
}


function getNowFormatDate(date) {
    //TODO 获取当前时间，格式YYYY-MM-DD
    // var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
}


function formatDate(now) {
    //TODO 时间戳转日期，后面的参为加减的时间分钟
    var gg = new Date(now);
    var year=gg.getFullYear();  //取得4位数的年份
    var month=gg.getMonth()+1;  //取得日期中的月份，其中0表示1月，11表示12月
    var date=gg.getDate();      //返回日期月份中的天数（1到31）
    var hour=gg.getHours();     //返回日期中的小时数（0到23）
    var minute=gg.getMinutes(); //返回日期中的分钟数（0到59）
    var second=gg.getSeconds(); //返回日期中的秒数（0到59）
    return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
}


function CurentTime() {
    //TODO 获取当前时间，格式YYYY-MM-DD hh:mm
    var now = new Date();
    var year = now.getFullYear();       //年
    var month = now.getMonth() + 1;     //月
    var day = now.getDate();            //日
    var hh = now.getHours();            //时
    var mm = now.getMinutes();          //分
    var clock = year + "-";

    if(month < 10)
        clock += "0";

    clock += month + "-";

    if(day < 10)
        clock += "0";

    clock += day + " ";

    if(hh < 10)
        clock += "0";

    clock += hh + ":";
    if (mm < 10) clock += '0';
    clock += mm;
    return(clock);
}


function writeCurrentDate() {
    //TODO 获取当前时间，格式YYYY-MM-DD hh:mm:ss
    var now = new Date();
    var year = now.getFullYear();       //年
    var month = now.getMonth() + 1;     //月
    var day = now.getDate();            //日
    var hh = now.getHours();            //时
    var mm = now.getMinutes();          //分
    var ss = now.getSeconds();          //秒
    var clock = year + "-";

    if(month < 10)
        clock += "0";

    clock += month + "-";

    if(day < 10)
        clock += "0";

    clock += day + " ";

    if(hh < 10)
        clock += "0";

    clock += hh + ":";
    if (mm < 10) clock += '0';
    clock += mm;

    if(ss < 10)
        clock += "0";

    clock = clock+':'+ss;

    return(clock);
}


function diffDays(strDateStart,strDateEnd){
    var strSeparator = "-"; //日期分隔符
    var oDate1;
    var oDate2;
    var iDays;
    oDate1= strDateStart.split(strSeparator);
    oDate2= strDateEnd.split(strSeparator);
    var strDateS = new Date(oDate1[0], oDate1[1]-1, oDate1[2]);
    var strDateE = new Date(oDate2[0], oDate2[1]-1, oDate2[2]);
    iDays = parseInt(Math.ceil(strDateS - strDateE ) / 1000 / 60 / 60 /24);//把相差的毫秒数转换为天数
    return iDays ;
}


function  getSysDate(){
    //TODO 获取系统时间
    var time = new Date();
    var myYear = time.getFullYear();
    var myMonth = time.getMonth()+1;
    var myDay = time.getDate();
    if(myMonth < 10){
        myMonth = "0" + myMonth;
    }
    if(myDay < 10){
        myDay = "0" + myDay;
    }
    //document.getElementById("yue_fen").innerHTML =  myYear + "." + myMonth;
    // document.getElementById("day_day").innerHTML =  myYear + "." + myMonth + "." + myDay;
    return myYear + "-" + myMonth + "-" + myDay
    // $('.Sysday').text(myYear + "-" + myMonth + "-" + myDay)
}


function  getSysMonthEnd(){
    //TODO 获取系统时间—最后一天
    var time = new Date();
    var myYear = time.getFullYear();
    var myMonth = time.getMonth()+1;
    var myDay = time.getDate();
    if(myMonth < 10){
        myMonth = "0" + myMonth;
    }
    if(myDay < 10){
        myDay = "0" + myDay;
    }
    var lastDay = new Date(time.getFullYear(), time.getMonth() + 1, 0);
    var d = new Date(lastDay);
    // console.log(d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate())
    return d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
}


function  getSysMonthStart(){
    //TODO 获取系统时间-01号
    var time = new Date();
    var myYear = time.getFullYear();
    var myMonth = time.getMonth()+1;
    var myDay = time.getDate();
    if(myMonth < 10){
        myMonth = "0" + myMonth;
    }
    if(myDay < 10){
        myDay = "0" + myDay;
    }
    var sysmonth = myYear + "-" + myMonth + "-01";
    return sysmonth
    //document.getElementById("yue_fen").innerHTML =  myYear + "." + myMonth;
    // document.getElementById("day_day").innerHTML =  myYear + "." + myMonth + "." + myDay;
    // $('.Sysday').text(myYear + "-" + myMonth + "-" + myDay)
}

function getLastDay(year,month) {
    //TODO 获取某年某月最后一天
    var new_year = year;    //取当前的年份
    var new_month = month++;//取下一个月的第一天，方便计算（最后一天不固定）
    if(month>12) {
        new_month -=12;        //月份减
        new_year++;            //年份增
    }
    var new_date = new Date(new_year,new_month,1);                //取当年当月中的第一天
    return (new Date(new_date.getTime()-1000*60*60*24)).getDate();//获取当月最后一天日期
}

function datemonth(date1,date2){
    //TODO //计算日期之间的月数
    //用-分成数组
    date1 = date1.split("-");
    date2 = date2.split("-");
    // 获取年,月数
    var year1 = parseInt(date1[0]),
        month1 = parseInt(date1[1]),
        year2 = parseInt(date2[0]),
        month2 = parseInt(date2[1]),
    //通过年,月差计算月份差
        months = (year2 - year1) * 12 + (month2 - month1) + 1;
    return months;
}


function randomWord(randomFlag, min, max){
    //TODO 生成随机字符串
    /*
    ** randomWord 产生任意长度随机字母数字组合
    ** randomFlag-是否任意长度 min-任意长度最小位[固定位数] max-任意长度最大位
    ** xuanfeng 2014-08-28
    */
    var str = "",
        range = min,
        arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
    // 随机产生
    if(randomFlag){
        range = Math.round(Math.random() * (max-min)) + min;
    }
    for(var i=0; i<range; i++){
        pos = Math.round(Math.random() * (arr.length-1));
        str += arr[pos];
    }
    return str;
}



function randomNum(minNum,maxNum){
    //TODO //生成从minNum到maxNum的随机数
    switch(arguments.length){
        case 1:
            return parseInt(Math.random()*minNum+1,10);
            break;
        case 2:
            return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10);
            break;
        default:
            return 0;
            break;
    }
}


//下载
function downloadFile(fileName, content) {
    //  fileName：文件名称
    //  content：base64码
    var aLink = document.createElement('a');
    var blob = base64ToBlob(content); //new Blob([content]);

    var evt = document.createEvent("HTMLEvents");
    evt.initEvent("click", true, true);//initEvent 不加后两个参数在FF下会报错  事件类型，是否冒泡，是否阻止浏览器的默认行为
    aLink.download = fileName;
    aLink.href = URL.createObjectURL(blob);

    // aLink.dispatchEvent(evt);
    //aLink.click()
    aLink.dispatchEvent(new MouseEvent('click', {bubbles: true, cancelable: true, view: window}));//兼容火狐
}



function base64ToBlob(code) {
    var parts = code.split(';base64,');
    var contentType = parts[0].split(':')[1];
    var raw = window.atob(parts[1]);
    var rawLength = raw.length;

    var uInt8Array = new Uint8Array(rawLength);

    for (var i = 0; i < rawLength; ++i) {
        uInt8Array[i] = raw.charCodeAt(i);
    }
    return new Blob([uInt8Array], {type: contentType});
}



function digitUppercase(n) {
    //TODO 金额转大写
    var fraction = ['角', '分'];
    var digit = [
        '零', '壹', '贰', '叁', '肆',
        '伍', '陆', '柒', '捌', '玖'
    ];
    var unit = [
        ['元', '万', '亿'],
        ['', '拾', '佰', '仟']
    ];
    var head = n < 0 ? '欠' : '';
    n = Math.abs(n);
    var s = '';
    for (var i = 0; i < fraction.length; i++) {
        s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
    }
    s = s || '整';
    n = Math.floor(n);
    for (var i = 0; i < unit[0].length && n > 0; i++) {
        var p = '';
        for (var j = 0; j < unit[1].length && n > 0; j++) {
            p = digit[n % 10] + unit[1][j] + p;
            n = Math.floor(n / 10);
        }
        s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
    }
    return head + s.replace(/(零.)*零元/, '元')
        .replace(/(零.)+/g, '零')
        .replace(/^整$/, '零元整');
}
